package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.meterService.bean.query.MsChgPlanQueryBean;
import com.koron.css2.meterService.dto.MsChgNewDto;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.SelectBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 快捷换表列表查询
 * @author admin
 * @version 2.1
 * @since 2021.10.15
 *
 */
public class MsChgListNew implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(MsChgListNew.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			MsChgPlanQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsChgPlanQueryBean.class);
			if(bean.getReceiptType() == null) {
				return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "拆换表类型不能为空", Void.class);
			}

			//营业区域单独处理
			if (bean.getBusinessArea() != null && !"".equals(bean.getBusinessArea())) {
				BusinessAreaMapper mapper = factory.getMapper(BusinessAreaMapper.class);
				List<String> areaIdList = queryChildrenArea(bean.getBusinessArea(), userInfo.getCurWaterCode(), mapper);
				bean.setBusinessAreaList(areaIdList);
			}


			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			MsChgPlanMapper mapper = factory.getMapper(MsChgPlanMapper.class);	
			List<MsChgNewDto> list = mapper.selectNewMsChgPlanList(bean);


			//营业区域
			List<SelectVO> businessAreaList = factory.getMapper(BusinessAreaMapper.class).select(null, userInfo.getCurWaterCode());

			Map<String, String> replaceTypes = null;
			if(Objects.equals(2, bean.getReceiptType())) { //单据类型为拆表
				replaceTypes = Tools.mapDicByCode(factory, "MRM");//拆表类型
			}else if(Objects.equals(1, bean.getReceiptType())) { //单据类型为换表
				replaceTypes = Tools.mapDicByCode(factory, "MCM"); //换表类型
			}
			Map<String, String> handleWays = Tools.mapDicByCode(factory, "OMD"); //换表类型

			for(MsChgNewDto t : list) {
				t.setReplaceTypeName(replaceTypes.get(t.getReplaceType()));
				t.setReceiptTypeName(Objects.equals("1", t.getReceiptType()) ? "换表" : "拆表");
				t.setOldHandleWayName(handleWays.get(t.getOldHandleWay() + ""));

				for (SelectVO businessArea : businessAreaList) {
					if (businessArea.getId().equals(t.getBusinessArea())) {
						t.setBusinessArea(businessArea.getName());
						break;
					}
				}
			}
			info.setData(new PageInfo<MsChgNewDto>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("快捷换表列表查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

	public static List<String> queryChildrenArea(String parentId, String groupCode, BusinessAreaMapper mapper) {
		List<SelectBean> list = mapper.findBusinessAreaByParentId(parentId, groupCode);
		List<String> result = new ArrayList<>();
		if (list != null && list.size() > 0) {
			result.addAll(list.stream().map(SelectBean::getId).collect(Collectors.toList()));
		} else {
			result = null;
		}
		return result;
	}
}
